/***************************************
* Jerato Ver 0.1
* Copyright 2008 Beta.Ito
* MIT License
***************************************/
package com.googlecode.jerato.library;

import java.io.*;
import java.util.*;

import javax.xml.parsers.*;

import org.xml.sax.SAXException;

public class Settings extends StringHashMap{

	public static final String RESOURCE_EXTENTION_NAME = ".xml";

	protected static final long serialVersionUID = Settings.class.hashCode();
	
	protected SAXParserFactory _parserFactory = SAXParserFactory.newInstance();
	protected String _resourceName;
	protected String _resourcePath;
	protected boolean _ignoreNotFound;
	protected boolean _loaded;

	protected Settings(){
	}

	public Settings(Class targetClass){
		this(targetClass != null? targetClass.getName(): null);
	}
	public Settings(String resourceName){
		if(resourceName == null || resourceName.length() == 0){
			throw new IllegalArgumentException();
		}
		_resourceName = resourceName;
		_resourcePath = ValueUtility.getPathFromClass(resourceName);
//		_resourcePath = resourceName.replaceAll("\\.", "\\/");
		_resourcePath += RESOURCE_EXTENTION_NAME;
	}
	public Settings(String resourceName, String resourcePath){
		if(resourceName == null || resourceName.length() == 0){
			throw new IllegalArgumentException();
		}
		_resourceName = resourceName;
		_resourcePath = resourcePath;
	}

	public void setResourcePath(String path){
		_resourcePath = path;
	}
	
	public void load(){
		try{
			SAXParser saxParser = _parserFactory.newSAXParser();
			
			SettingsParser parser = new SettingsParser();
//			InputStream inputStream = ClassLoader.getSystemResourceAsStream(_resourcePath);
			InputStream inputStream = SystemService.getInstance().getResourceAsStream(_resourcePath);
			if(inputStream == null){
				if(_ignoreNotFound){
					return;
				}
				throw new SystemException("resource not found. resourcePath=" + _resourcePath);
			}
			saxParser.parse(inputStream, parser);
			Map map = parser.getSectionMap();
			Map itemMap = (Map)map.get(_resourceName);
			if(itemMap != null){
				putAll(itemMap);
//				_itemMap = itemMap;
			}
			
		}
		catch(SAXException se){
			throw new SystemException("parser failed.", se);
		}
		catch(ParserConfigurationException pe){
			throw new SystemException("parser failed.", pe);
		}
		catch(IOException ioe){
			throw new SystemException("resource input failed.", ioe);
		}
		_loaded = true;
	}

//	public String getString(String name){
//		String value = (String)_itemMap.get(name);
//		if(value == null){
//			throw new SystemException("setting not found. name=" + name);
//		}
//		return value;
//	}

	public static void main(String[] args) {
		Settings settings = new Settings(Settings.class);
		settings.load();
//		System.out.println("map=" + settings._itemMap);
	}
	public boolean isIgnoreNotFound() {
		return _ignoreNotFound;
	}
	public void setIgnoreNotFound(boolean notFound) {
		_ignoreNotFound = notFound;
	}

	public boolean isLoaded(){
		return _loaded;
	}
//	public void clear() {
//		_itemMap.clear();
//	}
//	public boolean containsKey(Object key) {
//		return _itemMap.containsKey(key);
//	}
//	public boolean containsValue(Object value) {
//		return _itemMap.containsValue(value);
//	}
//	public Set entrySet() {
//		return _itemMap.entrySet();
//	}
//	public Object get(Object key) {
//		return _itemMap.get(key);
//	}
//	public boolean isEmpty() {
//		return _itemMap.isEmpty();
//	}
//	public Set keySet() {
//		return _itemMap.keySet();
//	}
//	public Object put(Object arg0, Object arg1) {
//		return _itemMap.put(arg0, arg1);
//	}
//	public void putAll(Map arg0) {
//		_itemMap.putAll(arg0);
//	}
//	public Object remove(Object key) {
//		return _itemMap.remove(key);
//	}
//	public int size() {
//		return _itemMap.size();
//	}
//	public Collection values() {
//		return _itemMap.values();
//	}
}
